﻿using System;
using System.Data;
using System.Configuration;
using System.Collections.Generic;
using System.Text;
using Dcms.Common;
using Dcms.BLL;
using Dcms.Model;

public partial class Admin_MainNewFrom : System.Web.UI.Page
{
    private static readonly string WEB_PATH = ConfigurationManager.AppSettings["WebPath"];
    private static readonly string WEB_Language = ConfigurationManager.AppSettings["Language"];
    private static readonly string WEB_LanguageNmae = ConfigurationManager.AppSettings["LanguageNmae"];
    private static readonly string WEB_DefaultLanguage = ConfigurationManager.AppSettings["DefaultLanguage"];
    Dcms_PermissionBLL perBll = new Dcms_PermissionBLL();
    private int TreeCount = 0;
    private int userLevel;
    DataSet TreeDs = new DataSet();
    private StringBuilder divsb = new StringBuilder();
    CompetenceLib CL = new CompetenceLib();

    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            CL.IsLoginMain();
           userLevel=CL.GetUserLevel();
            if (!IsPostBack)
            {
                if (Request.QueryString["Language"] == null)
                {
                    if (Session["DCMSLANGUAGENAME"] == null)
                    {
                        String[] DelanAr = WEB_DefaultLanguage.Split(',');
                        Session.Add("DCMSLANGUAGE", DelanAr[0]);
                        Session.Add("DCMSLANGUAGENAME", DelanAr[1]);
                    }
                }
                else {
                    Session.Add("DCMSLANGUAGE", Request.QueryString["Language"].ToString());
                    Session.Add("DCMSLANGUAGENAME", Utils.UrlDecode(Request.QueryString["LangName"].ToString()));
                    Response.Redirect("MainFrom.aspx",false);
                }
                labLang.Text = Session["DCMSLANGUAGENAME"].ToString();

                String TempLang = "\t\t\t<div onMouseOver=\"this.className='over'\" onMouseOut=\"this.className=''\" onClick=\"location.href='{$HREF}'\">\n";
                TempLang += "\t\t\t\t<img src=\"images/icon-{$LANAGE}.gif\" align=\"absmiddle\" /> {$LANAGENAME}\n\t\t\t\t</div>\n";

                String[] lanAr = WEB_Language.Split(',');
                String[] lanNAr = WEB_LanguageNmae.Split(',');

                if (lanAr.Length > 0) {
                    String Lang = "";
                    for (int i = 0; i < lanAr.Length; i++) {
                        Lang += TempLang;
                        Lang = Lang.Replace("{$LANAGE}", lanAr[i]);
                        Lang = Lang.Replace("{$LANAGENAME}", lanNAr[i]);
                        Lang = Lang.Replace("{$HREF}", "?Language=" + lanAr[i] + "&LangName=" + Utils.UrlEncode(lanNAr[i]));
                    }

                    String TempStr = "<div class=\"list\" onMouseOver=\"MM_showHideLayers('Top_navig','','show')\" onMouseOut=\"MM_showHideLayers('Top_navig','','hide')\">\n";
                    TempStr += "\t<div id=\"Top_navig\" class=\"lan\">\n";
                    TempStr += Lang;
                    TempStr += "\t</div>";
                    TempStr += "\t<a href=\"#\">语言选择<img src=\"images/icon-other.gif\" align=\"absmiddle\" /></a>\n";
                    TempStr += "</div>\n";

                    Lit_Lang.Text = TempStr;
                }

                this.litName.Text = CL.getSessionModle().LoginName;

                //加载菜单
                string Language = "";
                if (Session["DCMSLANGUAGE"] != null)
                {
                    Language = Session["DCMSLANGUAGE"].ToString();
                }

                if (CL.getSessionModle().IsAdministrator)
                {
                    userLevel = 100;
                }
                   



                    Dcms.BLL.Dcms_CateBLL NewCateBLL = new Dcms_CateBLL();

                    TreeDs = NewCateBLL.GetList("Cate_Lang='" + Language + "' and Cate_State='1'");

                    divsb.AppendLine("<div class='dtree'>");

                    divsb.AppendLine("<script type='text/javascript'>");
                    divsb.AppendLine("//<!--");
                    divsb.AppendLine("d = new dTree('d');");


                    divsb.AppendLine("d.add(0,-1,'栏目管理','#');");
                    if (TreeDs != null && TreeDs.Tables[0] != null)
                    {

                        forTree(0, 0);

                    }
                    //divsb.AppendLine("d.add(100,-1,'系统管理','#');");
                    divsb.AppendLine("document.write(d);");

                    divsb.AppendLine(" d.openAll();");
                    divsb.AppendLine("//-->");
                    divsb.AppendLine("</script>");
                    divsb.AppendLine("</div>");
                    this.litMenu.Text = divsb.ToString();


               
            }
            
        }
          
        catch (Exception ex)
        {
            Log.LogHandler.LogError("Admin_MainFrom.Page_Load", ex);
            throw ex;
        }
    }
    /// <summary>
    /// 递归添加树节点
    /// </summary>
    private void forTree(int ParentID, int TreeParent)
    {
        foreach (DataRow dr in TreeDs.Tables[0].Rows)
        {
            if (CL.IsRead(Convert.ToInt32(dr["Cate_ID"])))
            {
                if (dr["Cate_ParentID"].ToString() == ParentID.ToString())
                {
                    ///浏览等级判定
                    //if (userLevel >= int.Parse(dr["Cate_browse"].ToString()))
                    //{
                    TreeCount = TreeCount + 1;
                    string url = dr["Cate_ViewURL"].ToString() + "?CatID=" + dr["Cate_ID"] + "&action=edit";
                    //if (url == "" || dr["Cate_OpType"].ToString() == "ADDONLY")
                    //{
                    //    url = "#";
                    //}
                    ////else
                    ////{
                    ////    url = "http://www.163.com";
                    ////}
                    //else
                    //{
                        url = "javascript:MM_OpenTabLayers(\'" + dr["Cate_Name"].ToString() + "\',\'" + url + "\',\'" + dr["Cate_ID"].ToString() + "\')";
                    //}
                    divsb.AppendLine("d.add(" + TreeCount + "," + TreeParent + ",\"" + dr["Cate_Name"].ToString() + "\",\"" + url + "\");");
                    forTree(int.Parse(dr["Cate_ID"].ToString()), TreeCount);
                    //}
                }
            }
        }
        return;
    }
    private string getTopCateID(string ModelFlag, int ModelKeyID, string strLanguage)
    {
        Dcms_CateBLL cBll = new Dcms_CateBLL();
        Dcms_CateModel cModel = cBll.GetModelByFlag(ModelFlag, ModelKeyID, strLanguage);
        if (cModel != null)
        {
            return "&TopCateID=" + cModel.Cate_ID;
        }
        else
        {
            return "";
        }
    }
}
